//
//  YLRegularFinancingTopView.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/13.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLRegularFinancingTopView.h"
#import "YLRegularFinancingProgressView.h"
#import "YLRegularFinancingViewModel.h"
#import <Masonry.h>

@interface YLRegularFinancingTopView()
@property (nonatomic, strong) NSMutableArray *progresses;
@property (nonatomic, strong) YLRegularFinancingViewModel *viewModel;
@end

@implementation YLRegularFinancingTopView


- (YLRegularFinancingViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLRegularFinancingViewModel alloc] init];
    }
    return _viewModel;
}

- (NSMutableArray *)progresses
{
    if (!_progresses) {
        _progresses = [NSMutableArray arrayWithCapacity:2];
    }
    return _progresses;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setUp];
    }
    return self;
}


- (void)setUp
{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    [self addSubview:scrollView];
   
    CGFloat w = self.frame.size.width;
    
    CGFloat h = self.frame.size.height;
    scrollView.contentSize = CGSizeMake(w * 2, 0);
    scrollView.pagingEnabled = YES;
    for (NSInteger i = 0; i < 2; i ++) {
        CGFloat x = i * w;
        YLRegularFinancingProgressView *progress = [[YLRegularFinancingProgressView alloc] initWithFrame:CGRectMake(x, 0, w, h)];
        
        if (i == 0) {
            progress.leftButton.hidden = YES;
            progress.rightButton.hidden = NO;
            [progress.toRightSubject subscribeNext:^(id  _Nullable x) {
                [UIView animateWithDuration:0.3 animations:^{
                    [scrollView setContentOffset:CGPointMake(w, 0)];
                }];
            }];
        } else {
            progress.rightButton.hidden = YES;
            progress.leftButton.hidden = NO;
            [progress.toLeftSubject subscribeNext:^(id  _Nullable x) {
                [UIView animateWithDuration:0.3 animations:^{
                    [scrollView setContentOffset:CGPointZero];
                }];
            }];
        }
        [self.progresses addObject:progress];
        [scrollView addSubview:progress];
        
    }
    
    
    __weak typeof(self) weakSelf = self;
    [[self.viewModel.progressCommand execute:nil] subscribeNext:^(id  _Nullable x) {
        
        for (NSInteger i = 0; i < weakSelf.progresses.count; i ++) {
            YLRegularFinancingProgressView *progressView = self.progresses[i];
            progressView.group = weakSelf.viewModel.models[i];
        }
    }];
    
    
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    for (YLRegularFinancingProgressView *progress in self.progresses) {
            progress.leftButton.hidden = YES;
            progress.rightButton.hidden = YES;
    }
}

//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
//{
//    NSUInteger index = scrollView.contentOffset.x / YLScreenW;
//    if (self.progresses.count < index) {
//        return;
//    }
//    
//    YLRegularFinancingProgress *progress = self.progresses[index];
//    if (index == 0) {
//        progress.rightButton.hidden = NO;
//        progress.leftButton.hidden = YES;
//    } else {
//        progress.rightButton.hidden = YES;
//        progress.leftButton.hidden = NO;
//    }
//}
//
//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
//{
//    NSUInteger index = scrollView.contentOffset.x / YLScreenW;
//    if (self.progresses.count < index) {
//        return;
//    }
//    
//    YLRegularFinancingProgress *progress = self.progresses[index];
//    if (index == 0) {
//        progress.rightButton.hidden = NO;
//        progress.leftButton.hidden = YES;
//    } else {
//        progress.rightButton.hidden = YES;
//        progress.leftButton.hidden = NO;
//    }
//}


@end
